/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import java.io.File;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

/**
 *
 * @author Bughi
 */
public abstract class Controlador {

    private HttpServletRequest request;
    private HttpServletResponse response;
    protected ServletContext contexto;
    protected Resposta resposta;
    protected Map<String, String[]> parametrosEntrada;
    protected Map<String, FileItem> arquivosEntrada;

    public Controlador(HttpServletRequest request, HttpServletResponse response, ServletContext contexto) {
        this.request = request;
        this.response = response;
        this.resposta = new Resposta(request, response);
        
        this.contexto = contexto;

        parametrosEntrada = new HashMap<String,String[]>();
        arquivosEntrada = new HashMap<String,FileItem>();

        boolean isMultipart = ServletFileUpload.isMultipartContent(request);
        if (isMultipart) {
            DiskFileItemFactory factory = new DiskFileItemFactory();
            factory.setSizeThreshold(8 * 1024);
            factory.setRepository(new File(System.getProperty ("java.io.tmpdir")));
            ServletFileUpload upload = new ServletFileUpload(factory);
            upload.setSizeMax(20000 * 1024);
            try {
                // Parse the request to get file items.
                List fileItems = upload.parseRequest(request);
                

                // Process the uploaded file items
                Iterator i = fileItems.iterator();

                while (i.hasNext()) {
                    FileItem fi = (FileItem) i.next();
                    if (!fi.isFormField()) {
                        arquivosEntrada.put(fi.getFieldName(), fi);
                    } else {
                        String[] valor = {fi.getString()};
                        parametrosEntrada.put(fi.getFieldName(),valor);
                    }
                }
            } catch (Exception ex) {
                System.out.println(ex);
            }
        } else {
            Enumeration<String> pars = request.getParameterNames();
            while (pars.hasMoreElements()) {
                String par = pars.nextElement();
                parametrosEntrada.put(par, request.getParameterValues(par));
            }
        }
    }
    
    public String getParameterValue(String nome){
        return (this.parametrosEntrada.get(nome) != null)?this.parametrosEntrada.get(nome)[0]:null;
    }

    public String[] getParameterValues(String nome){
        return (this.parametrosEntrada.get(nome) != null)?this.parametrosEntrada.get(nome):null;
    }
    
    public FileItem getUploadedFile(String nome){
        return this.arquivosEntrada.get(nome);
    }

    public void responder() throws ServletException, IOException {
        this.resposta.executar();
    }

    /**
     * @return the response
     */
    public HttpServletResponse getResponse() {
        return response;
    }

    /**
     * @param response the response to set
     */
    public void setResponse(HttpServletResponse response) {
        this.response = response;
    }

    /**
     * @return the request
     */
    public HttpServletRequest getRequest() {
        return request;
    }
}
